package com.asion.qkan.adapters;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.asion.qkan.R;
import com.asion.qkan.entry.AllContentBean;
import com.asion.qkan.ui.contract.AllContentContract;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * Created by HCJ on 2016/10/16.
 */

public class AllContentRecyAdapter extends RecyclerView.Adapter<AllContentRecyAdapter.ViewHolder>{
    private Context context;
    private List<AllContentBean.DataBean.ComicsBean> list;

    private AllContentContract.OnContentItemClickListener onContentItemClickListener;

    public void setOnContentItemClickListener(AllContentContract.OnContentItemClickListener onContentItemClickListener) {
        this.onContentItemClickListener = onContentItemClickListener;
    }

    public AllContentRecyAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    public void addAll(Collection collection){
        list.addAll(collection);
        notifyDataSetChanged();
    }
    public void reverse(){
        Collections.reverse(list);
        notifyDataSetChanged();
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_all_content_recy, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        AllContentBean.DataBean.ComicsBean comicsBean = list.get(position);
        holder.img.setImageURI(Uri.parse(comicsBean.getCover_image_url()));
        holder.tiltle.setText(comicsBean.getTitle());
        holder.count.setText(comicsBean.getLikes_count()/10000+"万");
        holder.itemView.setOnClickListener(v ->onContentItemClickListener.onContentItemClick(position,comicsBean));
    }

    @Override
    public int getItemCount() {
        return list == null?0:list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        private SimpleDraweeView img;
        private TextView tiltle;
        private TextView monthDay;
        private TextView count;
        public ViewHolder(View itemView) {
            super(itemView);
            img = (SimpleDraweeView) itemView.findViewById(R.id.item_all_content_image);
            tiltle = (TextView) itemView.findViewById(R.id.item_all_content_title);
            monthDay = (TextView) itemView.findViewById(R.id.item_all_content_month_day);
            count = (TextView) itemView.findViewById(R.id.item_all_content_likecount);
        }
    }
}
